package com.corecloud.tools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.corecloud.entity.Vocational;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    public static ImportResult readExcel(InputStream inputStream){
        ExcelListener listener = new ExcelListener();
        EasyExcel.read(inputStream, Vocational.class, listener).sheet().doRead();
        return listener.getImportResult();
    }

    private static class ExcelListener extends AnalysisEventListener<Vocational> {
        private final List<Vocational> successList = new ArrayList<>();
        private final List<Vocational> failList = new ArrayList<>();

        @Override
        public void invoke(Vocational data, AnalysisContext context) {
            if (data != null) {
                successList.add(data);
            } else {
                failList.add(data);
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            // 可以在这里进行一些后续处理
        }

        public ImportResult getImportResult() {
            return new ImportResult(successList, failList);
        }
    }

    public static class ImportResult {
        private final List<Vocational> successList;
        private final List<Vocational> failList;

        public ImportResult(List<Vocational> successList, List<Vocational> failList) {
            this.successList = successList;
            this.failList = failList;
        }

        public List<Vocational> getSuccessList() {
            return successList;
        }

        public List<Vocational> getFailList() {
            return failList;
        }
    }
}
